package com.jun.admin.core.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.util.WebAppRootListener;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.Set;

/**
 * @author yuejun.li
 * @date 2024/10/19 16:18
 */
@Configuration
public class WebSocketConfig implements ServletContainerInitializer {

    /**
     * ServerEndpointExporter 将会扫描所有使用 @ServerEndpoint 注解标记的类，并将它们注册为 WebSocket 服务端点
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    //在此修改websocket传输的限制，不修改，无法传输长字符串
    @Override
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        servletContext.addListener(WebAppRootListener.class);
        servletContext.setInitParameter("org.apache.tomcat.websocket.textBufferSize","1024000");
        servletContext.setInitParameter("org.apache.tomcat.websocket.binaryBufferSize", "1024000");
    }
}
